<?php
if (!$export) {
    function nf($value) {
        return number_format($value);
    }
    $date_picker->__dateFormat = "yy-mm-dd";
    echo "<scr" . "ipt type=\"text/javascript\">";
    $date_picker->generateDP("srcPeriodStart");
    $date_picker->generateDP("srcPeriodEnd");
    echo "</scr" . "ipt>";
    $date_picker->init();
} else {
    function nf($value) {
        return ($value);
    }
    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename="laporan_rugi_laba_per_akhir'.'_'. date('(d M Y)',strtotime($srcPeriodEnd)) . '.xls"');
    header('Pragma: no-cache');
    header('Expires: 0');
}
?>
<?php echo $breadcrumb; ?>

<link type="text/css" rel="stylesheet" href="<?php echo base_url(); ?>library/style_lap.css" />
<div id="content">
    <div class="DivTable">
        <table id="hor-minimalist-b" border="0">
            <tr>
                <td width="20%"></td>
                <td width="30%"></td>
                <td width="20%"></td>
                <td width="30%"></td>
            </tr>
            <?php
            if (!$export) {
                ?>
            <tr>  
                <td style="padding: 5px 0px;" align="left" colspan="3">
                    <form name="fCari" id="fCari" action="" method="get" >                        
                        <span style="font-size:12px">Dari</span>
                        <input class="read-only" type="text" name="srcPeriodStart" id="srcPeriodStart" value="<?php echo $srcPeriodStart; ?>" maxlength="10" size="10" readonly="readonly"  />                        
                        <span style="font-size:12px">Sampai</span>
                        <input class="read-only" type="text" name="srcPeriodEnd" id="srcPeriodEnd" value="<?php echo $srcPeriodEnd; ?>" maxlength="10" size="10" readonly="readonly"  />
                        <input type="submit" value="cari">
                    </form>                    
                </td>
                <td align="right">
                    <form name="fCari" id="fCari" action="" method="post">
                        <input type="hidden" name="export" value="1">
                        <input type="hidden" name="srcPeriodEnd" value="<?=$srcPeriodEnd?>">
                        <input type="hidden" name="srcPeriodStart" value="<?=$srcPeriodStart?>">
                        <input type="submit" value="export">
                    </form>
                </td>
            </tr>
                <?php } ?>
            <tr>
                <td colspan="4" align="center">PT ANUGERAH SETIA PERKASA</td>
            </tr>
            <tr>
                <td colspan="4" align="center">LAPORAN RUGI LABA</td>
            </tr>
            <tr>
                <td colspan="4" align="center">Untuk Periode yang berakhir <?php if ($srcPeriodEnd <> "") echo date('(d M Y)',strtotime($srcPeriodEnd)); else echo date('(d M Y)',strtotime('today'));?></td>
            </tr>
            <?php
            function generateData($arr_data, &$num, &$grand_total, $jenis) {
                $current = "";
                $total = 0;                
                foreach($arr_data as $row) {

                    if ($current <> $row->jenis) {
                        if ($current <> "") {
                            echo "
                        <tr>
                            <td></td>
                            <td>(Jumlah $current)</td>
                            <td align='right'></td>
                            <td align='right'>Rp $jenis".nf($total)."</td>
                        </tr>
                        ";
                        }
                        echo "
                        <tr>
                            <td>".$num.". ".$row->jenis."</td>
                            <td></td>
                            <td align='right'></td>
                            <td align='right'></td>
                        </tr>
                        ";
                        $total = 0;
                        $current = $row->jenis;
                        $num++;
                    }
                    echo "
                        <tr>
                            <td></td>
                            <td>- ".$row->sub_jenis."</td>
                            <td align='right'>Rp ".nf($row->total)."</td>
                            <td align='right'></td>
                        </tr>
                        ";
                    $total += $row->total;
                }
                echo "
                        <tr>                            
                            <td></td>
                            <td>(Jumlah $current)</td>
                            <td align='right'></td>
                            <td align='right'>Rp $jenis".nf($total)."</td>
                        </tr>
                        ";
                if ($jenis == "-"){
                    $grand_total -= $total;
                } else {
                    $grand_total += $total;
                }
            }
            $grand_total = 0;
            $num = 1;
            generateData($total_penjualan, $num, $grand_total, "");
            generateData($beban_penjualan, $num, $grand_total, "-");
            $grand_total -= $diskon;
            echo "
                        <tr>
                            <td>Diskon Penjualan</td>
                            <td></td>
                            <td align='right'></td>
                            <td align='right'>Rp -".nf($diskon)."</td>
                        </tr>
                        ";
            echo "
                        <tr>
                            <td><b>Laba Kotor</b></td>
                            <td></td>
                            <td align='right'></td>
                            <td align='right'>Rp ".nf($grand_total)."</td>
                        </tr>
                        ";
            generateData($operasional, $num, $grand_total, "-");
            echo "
                        <tr>
                            <td><b>Laba Bersih</b></td>
                            <td></td>
                            <td align='right'></td>
                            <td align='right'>Rp ".nf($grand_total)."</td>
                        </tr>
                        ";
            ?>
        </table>
    </div>
    <br class="bersih" />
</div>